package com.yehun.utils;

import java.io.*;

/**
 * Created by Genius on 2017/3/27 0027.
 */
public class FileUtil {

    private static File file = null;

    /**
     * 文件是否存在
     *
     * @param pathname
     * @return
     */
    public static boolean isExists(String pathname) {
        file = new File(pathname);
        return file.exists();
    }

    /**
     * 创建目录
     *
     * @param pathname
     */
    public static void createDirectory(String pathname) {
        file = new File(pathname);
        if (!file.isDirectory() && !file.exists()) {
            file.mkdirs();
        }
    }

    /**
     * 创建文件
     *
     * @param pathname
     */
    public static void createFile(String pathname) {
        try {
            file = new File(pathname);
            if (!file.exists())
                file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建文件
     *
     * @param pathname
     * @param content
     */
    public static void createFile(String pathname, String content) {
        createFile(pathname, content, false);
    }

    /**
     * 创建文件
     *
     * @param pathname
     * @param content
     * @param isappend
     */
    public static void createFile(String pathname, String content, boolean isappend) {
        try {
            file = new File(pathname);
            if (!file.exists())
                file.createNewFile();
//			FileWriter filewriter = new FileWriter(file, isappend);
//			filewriter.write(content);
//			filewriter.close();
            FileOutputStream out = new FileOutputStream(file, isappend);
            out.write(content.getBytes("utf-8"));// 娉ㄦ剰闇瑕佽浆鎹㈠搴旂殑瀛楃闆
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取文件
     *
     * @param pathname
     * @return
     */
    public static String readerFile(String pathname) {
        return readerFile(pathname, "utf-8");
    }

    /**
     * 读取文件
     *
     * @param pathname
     * @return
     */
    public static String readerFile(String pathname, String encoding) {
        StringBuilder content = new StringBuilder();
        try {
            file = new File(pathname);
            if (file.isFile() && file.exists()) {
                // InputStreamReader read = new InputStreamReader(new
                // FileInputStream(file), encoding);
                // BufferedReader bufferedReader = new BufferedReader(read);
                // String line = null;
                // while ((line = bufferedReader.readLine()) != null) {
                // content.append(line);
                // }
                FileReader reader = new FileReader(file);
                int index = 0;
                while ((index = reader.read()) != -1) {
                    content.append((char) index);
                }
                reader.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }

    /**
     * 获得文件类型
     *
     * @param pathname
     * @return 1目录|2文件|0未知
     */
    public static int isType(String pathname) {
        file = new File(pathname);
        return file.isDirectory() ? 1 : (file.isFile() ? 2 : 0);
    }

    /**
     * 获取指定目录下的list
     *
     * @param pathname
     * @return
     */
    public static String[] list(String pathname) {
        file = new File(pathname);
        return file.list();
    }


    /**
     * 删除文件
     *
     * @param pathname
     * @throws FileNotFoundException
     */
    public static void delete(String pathname) {
        file = new File(pathname);
        if (!file.exists())
            file.delete();
    }



}
